const {
	Service
} = require('uni-cloud-router')
const uniID = require("uni-id");
module.exports = class UserService extends Service {
	//登录
	async login(post) {
		let res = {}
		const {
		  username,
		  password,
		  needPermission
		} = post
		res = await uniID.login({
		  username,
		  password,
		  needPermission,
		  // 不指定queryField的情况下只会查询username
		  queryField: ['username', 'email', 'mobile']
		});
		return res
	}
	
	//注册
	async register(post) {
		 let res = {}
		const {
		  username,
		  password,
		  needPermission
		} = post
		res = await uniID.register({
		  username,
		  password,
		  needPermission
		});
		return res
	}
	//登出
	async logout(post) {
		const { ctx, service } = this;
		let res = {};
		res = await uniID.logout(ctx.event.uniIdToken);
		return res
	}
	//修改密码
	async updatePwd(post) {
		const { ctx, service } = this;
		let res = {};
		const {
		  uid,
		  oldPassword,
		  newPassword
		} = post
		res = await uniID.updatePwd({
		  uid,
		  oldPassword,
		  newPassword
		});
		return res
	}
	//重置密码
	async resetPwd(post) {
		let res = {};
		const {
		  uid,
		} = post
		res = await uniID.resetPwd({
		  uid: uid,
		  password: '1234@qwer'
		});
		return res
	}
	//修改用户信息
	async updateUser(post) {
		let res = {};
		// const {
		// 	uid,
		// 	email,
		// 	email_confirmed,
		// 	station,
		// 	mobile,
		// 	mobile_confirmed
		// } = post
		res = await uniID.updateUser(post);
		return res
	}
	//绑定用户为超级管理员权限的方法
	async bindRole(post) {
		let res = {};
		const {roleList} = await uniID.getRoleList({
			offset:0,
			limit:10,
			needTotal:true
		  });
		const {
			uid,
		} = post
		res = await uniID.bindRole({
			uid,
			roleList,
			reset:true
		});
		return res
	}
	//获取用户信息
	async getUser(post) {
		let res = {};
		const {
			uid,
			email,
			email_confirmed,
			station,
			mobile,
			mobile_confirmed
		} = post
		res = await uniID.getUserInfo({
			uid
		  })
		return res
	}
	//根据角色查询用户
	async getUserByRole(post) {
		// const role_id = 'admin' //默认超级管理员id
		const {role_id} = post
		const db = uniCloud.database();
		let res = await db.collection('uni-id-users').field({ '_id': true,'username':true}).where({'role.role_id':role_id}).get()
		  if (res.affectedDocs) {
			  return {code: 0,data:res.data, msg: '获取成功'}  
		  } else {  
			  return {code: 1, msg: '获取失败'}  
		  }  
	
	}
	//判断用户是否是超级管理员
	async isAdminByUser(post) {
		// const role_id = 'admin' //默认超级管理员id
		const {uid} = post
		const db = uniCloud.database();
		// 获取超级管理员列表
		const adminList = await this.service.common.user.getUserByRole({role_id: 'admin'});
		let ifAdmin = false;
		if(adminList.code === 0) {
			for(let i in adminList.data) {
				if(adminList.data[i]._id === uid) {
					ifAdmin = true;
					break;
				}
			}
			 return {code: 0,data:{ifAdmin}, msg: '获取成功'}  
		}else{
			return {code: 1,data:{ifAdmin:false}, msg: '获取失败'}
		}
		
		 
	}
	//公共校验token方法
	async checkToken() {
	   const { ctx } = this;
	   const auth = await uniID.checkToken(ctx.event.uniIdToken);
	   // 调用 service 进行业务处理
		return auth
	}
}
